<?php
/**
 * 飞书登录 - 切换启用/禁用状态
 */

header('Content-Type: application/json');

$input = file_get_contents('php://input');
$data = json_decode($input, true);

if (!isset($data['enabled'])) {
    echo json_encode(['success' => false, 'message' => '缺少必要参数']);
    exit;
}

$enabled = (bool)$data['enabled'];

// 读取现有配置
$configFile = __DIR__ . '/../../config.php';
if (!file_exists($configFile)) {
    echo json_encode(['success' => false, 'message' => '配置文件不存在']);
    exit;
}

$config = include $configFile;

// 更新飞书插件状态
if (!isset($config['plugins']['feishu'])) {
    $config['plugins']['feishu'] = [
        'enabled' => false,
        'app_id' => '',
        'app_secret' => '',
        'redirect_uri' => ''
    ];
}

$config['plugins']['feishu']['enabled'] = $enabled;

// 保存配置
$configContent = "<?php\n/**\n * 氚云API管理系统配置文件\n * Alyr.net 企业级配置管理\n */\n\nreturn " . var_export($config, true) . ";\n";

if (file_put_contents($configFile, $configContent)) {
    echo json_encode([
        'success' => true,
        'message' => $enabled ? '飞书登录已启用' : '飞书登录已禁用'
    ]);
} else {
    echo json_encode(['success' => false, 'message' => '保存配置失败']);
}
